import 'package:compact_dio/compact_dio.dart';

import 'exception_handler.dart';

/// A singleton class that provides a convenient way to configure and use CompactDio.
class FlutterCompactDio {
  static FlutterCompactDio? _instance;

  /// Returns the instance of [FlutterCompactDio].
  static FlutterCompactDio get config => _instance ??= FlutterCompactDio._();

  FlutterCompactDio._();

  /// Enables or disables loading indicator for network requests.
  FlutterCompactDio enableLoading(bool loading) {
    CompactDioProxy.instance.enableLoading(loading);
    return this;
  }

  /// Enables or disables logging of network requests and responses.
  FlutterCompactDio enableLog(bool enable) {
    CompactDioProxy.instance.enableLog(enable);
    return this;
  }

  /// Sets the default decoder for decoding response data.
  FlutterCompactDio setDefaultDecoder(BaseDecoder decoder) {
    CompactDioProxy.instance.setDefaultDecoder(decoder);
    return this;
  }

  /// Sets the base URL for all network requests.
  FlutterCompactDio setBaseUrl(String url) {
    CompactDioProxy.instance.setBaseUrl(url);
    return this;
  }

  /// Sets the connection timeout for network requests.
  FlutterCompactDio setConnectTimeout(int timeout) {
    CompactDioProxy.instance.setConnectTimeout(timeout);
    return this;
  }

  /// Sets the receive timeout for network requests.
  FlutterCompactDio setReceiveTimeout(int timeout) {
    CompactDioProxy.instance.setReceiveTimeout(timeout);
    return this;
  }

  /// Sets the send timeout for network requests.
  FlutterCompactDio setSendTimeout(int timeout) {
    CompactDioProxy.instance.setSendTimeout(timeout);
    return this;
  }

  /// Sets the headers for all network requests.
  FlutterCompactDio setHeaders(Map<String, dynamic> headers) {
    CompactDioProxy.instance.setHeaders(headers);
    return this;
  }

  /// Adds an interceptor to intercept network requests and responses.
  FlutterCompactDio addInterceptor(Interceptor interceptor) {
    CompactDioProxy.instance.addInterceptor(interceptor);
    return this;
  }

  /// Enables or disables connectivity check before making network requests.
  FlutterCompactDio enableCheckConnectivity(bool enable) {
    CompactDioProxy.instance.enableCheckConnectivity(enable);
    return this;
  }

  /// Sets the exceptionHandler for handling Dio exceptions.
  FlutterCompactDio setExceptionHandler(ExceptionHandler handler) {
    CompactDioProxy.instance.setExceptionHandler(handler);
    return this;
  }

  /// Creates the instance of [CompactDio] and initializes it with the configured options.
  void create() => CompactDioProxy.instance.create();
}
